package com.app.qbake.util;

import com.wind.base.Globe;
import com.wind.base.repo.UserRepo;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by wind on 2018/3/14.
 */

public class HeaderInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {

        Request originalRequest=chain.request();
        String token= UserRepo.getInstance().get(Globe.sAppContext).getToken();
        Map<String,String> headers=Globe.sHeaderParams.getHeaders(token);
        Request.Builder builder=originalRequest.newBuilder();

        Set<Map.Entry<String,String>> entries=headers.entrySet();
        for (Map.Entry<String,String> entry:entries){
            builder.addHeader(entry.getKey(),entry.getValue());
        }
        builder.method(originalRequest.method(), originalRequest.body());

        Request request =builder.build();
        Response response=chain.proceed(request);

        return response;

    }
}
